package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.eyzhs.app.R;
import com.lepu.app.fun.my.MyAddressManageActivity;
import com.lepu.app.fun.my.bean.BeanMyAddressManage;

public class AdapterMyAddressManageList extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private MyAddressManageActivity mContext = null;
	private ArrayList<BeanMyAddressManage> mArrayList = null;

	public AdapterMyAddressManageList(MyAddressManageActivity context,
			ArrayList<BeanMyAddressManage> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_address_manage_activity_listitem, null);

			holder = new ViewHolder();

			TextView nameTextView = (TextView) convertView
					.findViewById(R.id.nameTextView);
			TextView mobileTextView = (TextView) convertView
					.findViewById(R.id.mobileTextView);
			TextView addressTextView = (TextView) convertView
					.findViewById(R.id.addressTextView);
			TextView defaultCheckAddressTextView = (TextView) convertView
					.findViewById(R.id.defaultCheckAddressTextView);
			TextView editTextView = (TextView) convertView
					.findViewById(R.id.editTextView);
			TextView deleteTextView = (TextView) convertView
					.findViewById(R.id.deleteTextView);

			holder.nameTextView = nameTextView;
			holder.mobileTextView = mobileTextView;
			holder.addressTextView = addressTextView;
			holder.defaultCheckAddressTextView = defaultCheckAddressTextView;
			holder.editTextView = editTextView;
			holder.deleteTextView = deleteTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanMyAddressManage item = mArrayList.get(position);
		
		holder.nameTextView.setText(item.ReceiverName);
		holder.mobileTextView.setText(item.ReceiverPhone);
		holder.addressTextView.setText(item.ReceiverAddress);
		
		if(!TextUtils.isEmpty(item.IsDefault) && item.IsDefault.equals("1"))
		{
			holder.defaultCheckAddressTextView.setSelected(true);
		}else{
			holder.defaultCheckAddressTextView.setSelected(false);
		}
		
		String defaultAddressTag = String.format("default_address_%d", position);
		holder.defaultCheckAddressTextView.setTag(defaultAddressTag);
		
		holder.defaultCheckAddressTextView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mContext.requestSetDefaultAddress(item,holder.defaultCheckAddressTextView,position);
			}
		});
		
		holder.editTextView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mContext.jumpNewAddress(false,item);
			}
		});
		
		holder.deleteTextView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mContext.requestDeleteAddress(position,item.AID);
			}
		});

		return convertView;
	}

	private static class ViewHolder {

		TextView nameTextView;
		TextView mobileTextView;
		TextView addressTextView;
		TextView defaultCheckAddressTextView;
		TextView editTextView;
		TextView deleteTextView;

	}
}
